package io.yizoo.system.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.elm.core.CoreController;
import io.yizoo.system.entity.sys_dict_data;
import io.yizoo.system.entity.sys_file;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/system/login/io")
public class login_ioController extends CoreController {

    @PostMapping("/GetData")
    public Object GetData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(sys_file.class)
                        .Where("and rel_id = #{rel_id}","rel_id")
                        .Where("and module_name = #{module_name}","module_name")
                        .OrderBy("do_time")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/Delete")
    public Object Delete(@RequestBody String jsonStr) {
        sys_file mod = Ctor.HMapper.GetSingle(sys_file.class,
                Ctor.DMaker(jsonStr).Selector
                        .From(sys_file.class)
                        .Where("and id=#{id}","id")
                        .GetFactor()
        );
        Ctor.Minior.delete(mod.getMiniio_name());
        int rows = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(sys_file.class)
                        .Where("and id=#{id}", "id")
                        .GetFactor());
        return JsonResult(rows);
    }

    @PostMapping("/Upload")
    public Object upload(@RequestParam("file") MultipartFile file
            ,@RequestParam("module_name") String module_name
            ,@RequestParam("rel_id") String rel_id)
    {
        String uuid = UUID.randomUUID().toString();
        String miniio_name=module_name+"/"+rel_id+"/"+uuid+"/"+file.getOriginalFilename();
        Object ret = Ctor.Minior.upload(file,miniio_name);
        ObjectNode node = (new ObjectMapper()).createObjectNode();
        node.put("id", uuid);
        node.put("miniio_name", miniio_name);
        node.put("module_name", module_name);
        node.put("rel_id", rel_id);
        node.put("file_name", module_name);
        node.put("origin_name", file.getOriginalFilename());
        node.put("size", file.getSize());
        node.put("do_user", Ctor.getCurrentUserNickName());
        node.put("do_time", LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        Ctor.CoreMapper.UInsert(
                Ctor.DMaker(node).Insertor
                        .Insert(sys_file.class)
                        .GetFactor()
        );

        return JsonResult(node);
    }

    @PostMapping("/UploadMul")
    public Object uploadMul(@RequestParam("file") MultipartFile[] files
            ,@RequestParam("module_name") String module_name
            ,@RequestParam("rel_id") String rel_id) throws Exception
    {
        List<ObjectNode> nodeList = new ArrayList<>();
        for (MultipartFile file : files) {
            String uuid = UUID.randomUUID().toString();
            String miniio_name=module_name+"/"+rel_id+"/"+uuid+"/"+file.getOriginalFilename();
            Object ret = Ctor.Minior.upload(file,miniio_name);

            ObjectNode node = (new ObjectMapper()).createObjectNode();
            node.put("id", uuid);
            node.put("miniio_name", miniio_name);
            node.put("module_name", module_name);
            node.put("rel_id", rel_id);
            node.put("file_name", module_name);
            node.put("origin_name", file.getOriginalFilename());
            node.put("size", file.getSize());
            node.put("do_user", Ctor.getCurrentUserNickName());
            node.put("do_time", LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
            Ctor.CoreMapper.UInsert(
                    Ctor.DMaker(node).Insertor
                            .Insert(sys_file.class)
                            .GetFactor()
            );
            nodeList.add(node);
        }
        return JsonResult(new KV("node_list", nodeList));
    }

    @GetMapping("/Viewer/{value}")
    public Object Viewer(@PathVariable("value") String value)
    {
        sys_file mod = Ctor.HMapper.GetSingle(sys_file.class,
                Ctor.DMaker().Selector
                        .putFactorItem("id",value)
                        .From(sys_file.class)
                        .Where("and id=#{id}","id")
                        .GetFactor()
        );

        return Ctor.Minior.read(mod.getMiniio_name(),mod.getOrigin_name());
    }

    @GetMapping("/Viewer/{value1}/{value2}")
    public Object Viewer(@PathVariable("value1") String module_name ,@PathVariable("value2") String rel_id,@RequestParam(value = "timestamp",required = false) String timestamp)
    {
        sys_file mod = Ctor.HMapper.GetSingle(sys_file.class,
                Ctor.DMaker().Selector
                        .putFactorItem("module_name",module_name)
                        .putFactorItem("rel_id",rel_id)
                        .From(sys_file.class)
                        .Where("and rel_id = #{rel_id}","rel_id")
                        .Where("and module_name = #{module_name}","module_name")
                        .GetFactor()
        );
        if(mod==null)
        {
            return null;
        }
        return Ctor.Minior.read(mod.getMiniio_name(),mod.getMiniio_name());
    }

    @PostMapping("/SelectFileList")
    public Object SelectFileList(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(sys_file.class)
                        .Where("and module_name=#{module_name} and rel_id=#{rel_id}","module_name","rel_id")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/SelectDictList")
    public Object SelectDictList(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(sys_dict_data.class)
                        .Where("and dict_type=#{dict_type}","dict_type")
                        .OrderBy("dict_sort")
                        .GetFactor());
        return JsonResult(ret);
    }

}
